package o;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.IBinder;
import android.text.TextUtils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class cgt {
    public static boolean ˋ(Activity activity) {
        return ActivityUtil.isAcitivtyNotValid(activity);
    }

    public static String ˊ(Activity activity) {
        String str = "";
        if (activity == null) {
            return str;
        }
        str = activity.getCallingPackage();
        if (TextUtils.isEmpty(str)) {
            return ˎ(activity);
        }
        return str;
    }

    private static String ˎ(Activity activity) {
        String ˊ;
        String str = "";
        try {
            Class cls = Class.forName("android.app.ActivityManager");
            Class cls2 = Class.forName("android.app.Activity");
            Class cls3 = Class.forName("android.app.IActivityManager");
            Method method = cls.getMethod("getService", new Class[0]);
            Method method2 = cls2.getMethod("getActivityToken", new Class[0]);
            String[] packagesForUid = activity.getPackageManager().getPackagesForUid(((Integer) cls3.getMethod("getLaunchedFromUid", new Class[]{IBinder.class}).invoke(method.invoke(ActivityManager.class, new Object[0]), new Object[]{(IBinder) method2.invoke(activity, new Object[0])})).intValue());
            if (packagesForUid == null || packagesForUid.length <= 0) {
                return str;
            }
            ˊ = ˊ(activity, packagesForUid);
            try {
                return TextUtils.isEmpty(ˊ) ? packagesForUid[0] : ˊ;
            } catch (ClassNotFoundException e) {
                Logger.ˎ("ActivityHelper", "Get caller package ClassNotFoundException");
                return ˊ;
            } catch (NoSuchMethodException e2) {
                Logger.ˎ("ActivityHelper", "Get caller package NoSuchMethodException");
                return ˊ;
            } catch (IllegalAccessException e3) {
                Logger.ˎ("ActivityHelper", "Get caller package IllegalAccessException");
                return ˊ;
            } catch (InvocationTargetException e4) {
                Logger.ˎ("ActivityHelper", "Get caller package InvocationTargetException");
                return ˊ;
            }
        } catch (ClassNotFoundException e5) {
            ˊ = str;
            Logger.ˎ("ActivityHelper", "Get caller package ClassNotFoundException");
            return ˊ;
        } catch (NoSuchMethodException e6) {
            ˊ = str;
            Logger.ˎ("ActivityHelper", "Get caller package NoSuchMethodException");
            return ˊ;
        } catch (IllegalAccessException e7) {
            ˊ = str;
            Logger.ˎ("ActivityHelper", "Get caller package IllegalAccessException");
            return ˊ;
        } catch (InvocationTargetException e8) {
            ˊ = str;
            Logger.ˎ("ActivityHelper", "Get caller package InvocationTargetException");
            return ˊ;
        }
    }

    private static String ˊ(Context context, String[] strArr) {
        Field declaredField;
        boolean isAccessible;
        boolean z;
        Field field;
        Throwable th;
        try {
            declaredField = Class.forName("android.app.Activity").getDeclaredField("mReferrer");
            try {
                isAccessible = declaredField.isAccessible();
            } catch (IllegalAccessException e) {
                z = false;
                field = declaredField;
                try {
                    Logger.ˎ("ActivityHelper", "Get Activity referrer IllegalAccessException");
                    if (field != null) {
                        field.setAccessible(z);
                    }
                    return null;
                } catch (Throwable th2) {
                    isAccessible = z;
                    declaredField = field;
                    th = th2;
                    if (declaredField != null) {
                        declaredField.setAccessible(isAccessible);
                    }
                    throw th;
                }
            } catch (NoSuchFieldException e2) {
                isAccessible = false;
                try {
                    Logger.ˎ("ActivityHelper", "Get Activity referrer NoSuchFieldException");
                    if (declaredField != null) {
                        declaredField.setAccessible(isAccessible);
                    }
                    return null;
                } catch (Throwable th3) {
                    th = th3;
                    if (declaredField != null) {
                        declaredField.setAccessible(isAccessible);
                    }
                    throw th;
                }
            } catch (ClassNotFoundException e3) {
                isAccessible = false;
                Logger.ˎ("ActivityHelper", "Get Activity referrer ClassNotFoundException");
                if (declaredField != null) {
                    declaredField.setAccessible(isAccessible);
                }
                return null;
            } catch (RuntimeException e4) {
                isAccessible = false;
                Logger.ˎ("ActivityHelper", "Get Activity referrer RuntimeException");
                if (declaredField != null) {
                    declaredField.setAccessible(isAccessible);
                }
                return null;
            } catch (Exception e5) {
                isAccessible = false;
                Logger.ˎ("ActivityHelper", "Get Activity referrer Exception");
                if (declaredField != null) {
                    declaredField.setAccessible(isAccessible);
                }
                return null;
            } catch (Throwable th4) {
                th = th4;
                isAccessible = false;
                if (declaredField != null) {
                    declaredField.setAccessible(isAccessible);
                }
                throw th;
            }
            try {
                declaredField.setAccessible(true);
                String str = (String) declaredField.get(context);
                for (String str2 : strArr) {
                    if (str2.equals(str)) {
                        if (declaredField != null) {
                            declaredField.setAccessible(isAccessible);
                        }
                        return str2;
                    }
                }
                if (declaredField != null) {
                    declaredField.setAccessible(isAccessible);
                }
            } catch (IllegalAccessException e6) {
                z = isAccessible;
                field = declaredField;
                Logger.ˎ("ActivityHelper", "Get Activity referrer IllegalAccessException");
                if (field != null) {
                    field.setAccessible(z);
                }
                return null;
            } catch (NoSuchFieldException e7) {
                Logger.ˎ("ActivityHelper", "Get Activity referrer NoSuchFieldException");
                if (declaredField != null) {
                    declaredField.setAccessible(isAccessible);
                }
                return null;
            } catch (ClassNotFoundException e8) {
                Logger.ˎ("ActivityHelper", "Get Activity referrer ClassNotFoundException");
                if (declaredField != null) {
                    declaredField.setAccessible(isAccessible);
                }
                return null;
            } catch (RuntimeException e9) {
                Logger.ˎ("ActivityHelper", "Get Activity referrer RuntimeException");
                if (declaredField != null) {
                    declaredField.setAccessible(isAccessible);
                }
                return null;
            } catch (Exception e10) {
                Logger.ˎ("ActivityHelper", "Get Activity referrer Exception");
                if (declaredField != null) {
                    declaredField.setAccessible(isAccessible);
                }
                return null;
            }
        } catch (IllegalAccessException e11) {
            z = false;
            field = null;
            Logger.ˎ("ActivityHelper", "Get Activity referrer IllegalAccessException");
            if (field != null) {
                field.setAccessible(z);
            }
            return null;
        } catch (NoSuchFieldException e12) {
            isAccessible = false;
            declaredField = null;
            Logger.ˎ("ActivityHelper", "Get Activity referrer NoSuchFieldException");
            if (declaredField != null) {
                declaredField.setAccessible(isAccessible);
            }
            return null;
        } catch (ClassNotFoundException e13) {
            isAccessible = false;
            declaredField = null;
            Logger.ˎ("ActivityHelper", "Get Activity referrer ClassNotFoundException");
            if (declaredField != null) {
                declaredField.setAccessible(isAccessible);
            }
            return null;
        } catch (RuntimeException e14) {
            isAccessible = false;
            declaredField = null;
            Logger.ˎ("ActivityHelper", "Get Activity referrer RuntimeException");
            if (declaredField != null) {
                declaredField.setAccessible(isAccessible);
            }
            return null;
        } catch (Exception e15) {
            isAccessible = false;
            declaredField = null;
            Logger.ˎ("ActivityHelper", "Get Activity referrer Exception");
            if (declaredField != null) {
                declaredField.setAccessible(isAccessible);
            }
            return null;
        } catch (Throwable th5) {
            th = th5;
            isAccessible = false;
            declaredField = null;
            if (declaredField != null) {
                declaredField.setAccessible(isAccessible);
            }
            throw th;
        }
        return null;
    }
}
